爬取网页报 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

您所在的位置:网站首页 python网页爬取 json 爬取网页报 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

爬取网页报 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

2023-07-24 08:13| 来源: 网络整理| 查看: 265

爬取网页报 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

当爬取中国彩票网时,request.get()后解析json时报 json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0),原码如下:

import requests url='http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice?name=ssq&issueCount=100' headers={ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'} res=requests.get(url,headers=headers) #发送请求,并把响应内容赋值到变量res里面 print(res) res.content.decode("utf-8") articles = res.json() print(articles)

尝试了网上的各种办法后,无法解决,后在header中加入一个参数referer后解决,可以分享给大家。

import requests url='http://www.cwl.gov.cn/cwl_admin/kjxx/findDrawNotice?name=ssq&issueCount=100' headers={ 'Referer': 'http://www.cwl.gov.cn/kjxx/ssq/kjgg/', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36'} res=requests.get(url,headers=headers) #发送请求,并把响应内容赋值到变量res里面 print(res) res.content.decode("utf-8") articles = res.json() print(articles)

查找网络上有各种解决办法,大致有如下几种情况:

解码问题。 可以使用 reps.content.decode(“utf-8”)进行解码。 2.增加cookie equests.get网址后报错,打印爬取到的信息显示为“网络可能存在问题,请您重试一下!”

原因为request.get()是独立的一次访问,没有携带前几个访问请求反馈后的状态,相当于把每次请求看成了独立浏览器去完成的,所以12306拒绝访问。

解决方法为增加cookie,直接写入header或者直接使用cookie,内容可以从自己浏览的网页中获得。 参考文章1 爬取12306余票信息报错:json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3